Kotlin
Control Flow: if, when, for, while
Swift
|
If Expression
|
// Traditional usage
var max = a
if (a < b) max = b
// With else
var max: Int
if (a > b) {
max = a
} else {
max = b
}
// As expression
val max = if (a > b) a else b
|
// Traditional usage
var max = a
if a < b { max = b }
// With else
var max: Int
if a > b {
max = a
} else {
max = b
}
let max = a > b ? a : b
|
val max = if (a > b) {
print("Choose a")
a
} else {
print("Choose b")
b
}
|
let max: Int
if a > b {
print("Choose a")
max = a
} else {
print("Choose b")
max = b
}
|
When Expression
|
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> { // Note the block
print("x is neither 1 nor 2")
}
}
|
switch x {
case 1:
print("x == 1")
case 2:
print("x == 2")
default:
print("x is neither 1 nor 2")
}
|
when (x) {
0, 1 -> print("x == 0 or x == 1")
else -> print("otherwise")
}
|
switch x {
case 0, 1:
print("x == 0 or x == 1")
default:
print("otherwise")
}
|
when (x) {
parseInt(s) -> print("s encodes x")
else -> print("s does not encode x")
}
|
switch x {
case is Int:
print("s encodes x")
default:
print("s does not encode x")
}
|
when (x) {
in 1..10 -> print("x is in the range")
in validNumbers -> print("x is valid")
!in 10..20 -> print("x is outside the range")
else -> print("none of the above")
}
|
switch x {
case 1...10:
print("x is in the range")
case let x where validNumbers.contains(x):
print("x is valid")
case let x where 10...20 ~= x:
print("x is outside the range")
default:
print("none of the above")
}
|
fun hasPrefix(x: Any) = when(x) {
is String -> x.startsWith("prefix")
else -> false
}
|
func hasPrefix(x: Any) -> Bool {
switch x {
case let x as String:
return x.starts(with: "prefix")
default:
return false
}
}
|
when {
x.isOdd() -> print("x is odd")
y.isEven() -> print("y is even")
else -> print("x+y is odd.")
}
|
if x.isOdd() {
print("x is odd")
}else if y.isEven() {
print("y is even")
}else {
print("x+y is odd.")
}
|
fun Request.getBody() =
when (val response = executeRequest()) {
is Success -> response.body
is HttpError -> throw HttpException(response.status)
}
|
extension Request {
func getBody() throws -> Body {
let response = executeRequest()
switch response {
case .success(let body):
return body
case .error(let status):
throw .httpException(status)
}
}
}
|
For Loops
|
for (item in collection) print(item)
|
for item in collection {
print(item)
}
|
for (item: Int in ints) {
// ...
}
|
for item in ints {
// ...
}
|
for (i in 1..3) {
println(i)
}
for (i in 6 downTo 0 step 2) {
println(i)
}
|
for i in 1...3 {
print(i)
}
for i in stride(from: 6, through: 0, by: -2) {
print(i)
}
|
for (i in array.indices) {
println(array[i])
}
|
for i in array.indices {
print(array[i])
}
|
for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
|
for (index, value) in array.enumerated() {
print("the element at \(index) is \(value)")
}
|
While Loops
|
while (x > 0) {
x--
}
do {
val y = retrieveData()
} while (y != null) // y is visible here!
|
while x > 0 {
x -= 1
}
var y: String?
repeat {
y = retrieveData()
} while y != nil
|